package com.haiyi.software.app;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import com.haiyi.software.dao.ChatDAO;
import com.haiyi.software.xmpp.event.XMPPEvent;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnClickListener,
		XMPPEvent {

	RadioButton nearbyRadioButton, infoRadioButton, contactsRadioButton,
			settingRadioButton;

	FragmentNearby fragmentNearby;

	FragmentMe FragmentMe;

	FragmentInformation fragmentInformation;

	FragmentDiscover fragmentDiscover;

	CheckBox[] CheckBoxs = new CheckBox[4];

	CheckBox rd_main_tb_nearby, rd_main_tb_info, rd_main_tb_contacts,
			rd_main_tb_setting;

	TextView textView_s2;

	MainApplication app;

	/**
	 * 用于对Fragment进行管理
	 */
	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		app = MainApplication.getInstance();

		app.addXMPPEvent(this);

		getParam();

		initComp();

		fragmentManager = getSupportFragmentManager();

		setTabSelection(0);

	}

	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		handler.sendEmptyMessageDelayed(1, 300);
	}

	private void getParam() {

	}

	private void initComp() {

		rd_main_tb_nearby = (CheckBox) this
				.findViewById(R.id.rd_main_tb_nearby);

		rd_main_tb_info = (CheckBox) this.findViewById(R.id.rd_main_tb_info);

		rd_main_tb_contacts = (CheckBox) this
				.findViewById(R.id.rd_main_tb_contacts);

		rd_main_tb_setting = (CheckBox) this
				.findViewById(R.id.rd_main_tb_setting);

		rd_main_tb_nearby.setOnClickListener(this);
		rd_main_tb_info.setOnClickListener(this);
		rd_main_tb_contacts.setOnClickListener(this);
		rd_main_tb_setting.setOnClickListener(this);

		CheckBoxs[0] = rd_main_tb_nearby;
		CheckBoxs[1] = rd_main_tb_info;
		CheckBoxs[2] = rd_main_tb_contacts;
		CheckBoxs[3] = rd_main_tb_setting;

		textView_s2 = (TextView) this.findViewById(R.id.textView_s2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次后退键退出程序",
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 注意
				intent.addCategory(Intent.CATEGORY_HOME);
				this.startActivity(intent);
				return true;
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void setTabSelection(int i) {

		// TODO 自动生成的方法存根
		FragmentTransaction transaction = fragmentManager.beginTransaction();

		hideFragments(transaction);

		switch (i) {
		case 0:
			if (fragmentNearby == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				fragmentNearby = new FragmentNearby();
				transaction.add(R.id.active_main_content, fragmentNearby);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fragmentNearby);
			}
			break;
		case 1:
			if (fragmentInformation == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				fragmentInformation = new FragmentInformation();
				transaction.add(R.id.active_main_content, fragmentInformation);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fragmentInformation);
			}
			break;
		case 2:
			if (fragmentDiscover == null) {
				fragmentDiscover = new FragmentDiscover();
				transaction.add(R.id.active_main_content, fragmentDiscover);
			} else {
				transaction.show(fragmentDiscover);
			}
			break;
		case 3:
			if (FragmentMe == null) {
				FragmentMe = new FragmentMe();
				transaction.add(R.id.active_main_content, FragmentMe);
			} else {
				transaction.show(FragmentMe);
			}
			break;
		default:
			break;
		}

		transaction.commit();
	}

	private void hideFragments(FragmentTransaction transaction) {

		// TODO 自动生成的方法存根
		if (fragmentDiscover != null) {
			transaction.hide(fragmentDiscover);
		}
		if (fragmentInformation != null) {
			transaction.hide(fragmentInformation);
		}
		if (FragmentMe != null) {
			transaction.hide(FragmentMe);
		}
		if (fragmentNearby != null) {
			transaction.hide(fragmentNearby);
		}
	}

	@Override
	public void onClick(View v) {

		for (CheckBox ck : CheckBoxs) {
			ck.setChecked(false);
		}

		((CheckBox) v).setChecked(true);

		// TODO 自动生成的方法存根
		switch (v.getId()) {
		case R.id.rd_main_tb_contacts:
			setTabSelection(2);
			break;
		case R.id.rd_main_tb_info:
			setTabSelection(1);
			break;
		case R.id.rd_main_tb_nearby:
			setTabSelection(0);
			break;
		case R.id.rd_main_tb_setting:
			setTabSelection(3);
			break;
		}
	}

	@Override
	public void XMPPConnectEvent(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPAuthenticate(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPNotAuthenticate(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPReceiveMessage(Message msg) {
		// TODO 自动生成的方法存根
		handler.sendEmptyMessage(1);
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(android.os.Message msg) {
			// TODO 自动生成的方法存根
			int cnt = 0;
			cnt = ChatDAO.getInstence().getUnreadCount();
			if (cnt > 0) {
				textView_s2.setVisibility(View.VISIBLE);
				if (cnt > 99) {

					textView_s2.setText("99+");
				} else {
					textView_s2.setText(cnt + "");
				}
			} else {
				textView_s2.setText("");
				textView_s2.setVisibility(View.GONE);
			}
		};

	};

}
